package com.lx.controller;

import com.lx.adapter.LoginAdapter;
import com.lx.dto.OAuth2AuthorizationCode;
import com.lx.dto.UserInfoResponseDTO;
import com.lx.service.IWeixinApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;

import javax.annotation.Resource;
import java.io.IOException;

@Slf4j
@RestController()
@CrossOrigin("*")
public class AuthorizationController {
    @Resource
    private LoginAdapter loginAdapter;

    @RequestMapping("/goback")
    public String back(OAuth2AuthorizationCode authorizationCode) {

        System.out.println(authorizationCode.toString());
        try {
            loginAdapter.getUserInfo(authorizationCode.getCode());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "xxx";
    }


    @RequestMapping("/getUserInfo")
    public String getUserInfo() throws IOException {

        IWeixinApiService iWeixinApiService = new Retrofit.Builder()
                .baseUrl("https://api.weixin.qq.com/")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(JacksonConverterFactory.create())
                .build()
                .create(IWeixinApiService.class);
        String accessToken = "85_4KyKaYcaTFUncz_K02qlx3NBj7GVWrUBh8KJvLjyou54Cecj8d0GviXB8MDj8hQnucVKuggnRPkziknEmqO9Ohe6gKWqD-MZJCTVp6RocQI";
        String openId = "orjxH6btgUKvxTcJchQknH0OIXPA";
        UserInfoResponseDTO resp = iWeixinApiService.getAuthorizationUserInfo(accessToken, openId, "zh_CN").execute().body();

        return resp.toString();
    }
}
